// pages/tuijian/tuijain.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tuijianGedan:[],//推荐歌单
    date:{month:'',day:''}
  },
  getDate(){
    let date = new Date();
    let m = (date.getMonth()+1).toString().padStart(2,'0');
    let d = date.getDate().toString().padStart(2,'0');
    this.setData({
      date:{month:m,day:d}
    })
  },
  getRecommend(){
    let data = wx.getStorageSync('tuijian') || null;
    console.log(data)
    if(data !== null && data.day !== this.data.date.day){
      this.removeRecommend() //不是当天的清空
      data = null
    }
    if(data == null){ //推荐数据没有，获取数据
      var reqTask = wx.request({
        url: 'http://musicapi.leanapp.cn/recommend/songs',
        data:{},
        header: {'content-type':'application/json'},
        method: 'GET',
        dataType: 'json',
        responseType: 'text',
        success: (result) => {
          if(result.data.code == 200){
            let recommend= result.data.recommend;
            this.saveRecommend(recommend);
            this.setData({
              tuijianGedan:recommend
            })
          }
        }
        
      });
    }else{
      this.setData({
        tuijianGedan:data.data
      })
    }
  },
  saveRecommend(data){
    let saveData = {
      day:this.data.date.day,
      data:data
    }
    wx.setStorageSync("tuijian", saveData);
  },
  removeRecommend(){
    wx.removeStorageSync('tuijian');
  },
  songs(e){
      if(e.target.dataset.key){
        wx.navigateTo({
          url: '../song/song?mid='+e.target.dataset.key,
        });
      } 
  },
  //将全部歌曲信息放进列表中
  playAll(){
    wx.removeStorageSync("allPlay");
    let songs = this.data.tuijianGedan;
    let allSongs =[];
    for (let i in songs) {
      let data = {
        id:songs[i].id,
        name:songs[i].name,
        nama_tro:songs[i].alias[0],
      }
      allSongs.push(data)
    }
    wx.setStorageSync("allPlay",allSongs);
    wx.navigateTo({
      url: '../song/song?mid='+allSongs[0].id,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   this.getDate();
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getRecommend();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})